package com.test.mina2;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.LineDelimiter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MinaServer {

	private static Logger logger = LoggerFactory.getLogger(MinaServer.class);
	private static Integer PORT = 9999;

	public static void main(String[] args) throws IOException {
		IoAcceptor acceptor = new NioSocketAcceptor();
		// 设置过滤器（使用Mina提供的文本换行符编解码器）
		acceptor.getFilterChain().addLast(
			"myChainFilter",
			new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"),
				LineDelimiter.WINDOWS.getValue(), LineDelimiter.WINDOWS.getValue())));
		// 设置缓冲区大小
		acceptor.getSessionConfig().setReadBufferSize(2048);
		// 读写通道10秒内无操作进入空闲状态
		acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
		// 绑定逻辑处理器
		acceptor.setHandler(new Demo1ServerHandler());
		// 绑定端口
		acceptor.bind(new InetSocketAddress(PORT));
		logger.info("服务端启动成功... 端口号为：" + PORT);
		System.out.println("服务端启动成功... 端口号为：" + PORT);
	}
}
